package com.yx.doodleview.util;

import android.app.Activity;
import android.text.TextUtils;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.yx.doodleview.bean.ShareInfo;

import java.io.File;

/**
 * @ClassName ShareUtils
 * @Description 分享工具
 * Created by yx on 2017/8/25 0025.
 */

public class ShareUtils {


    /**
     * @param activity
     * @param share_media
     * @param shareInfo
     * @param callBackListener
     */
    public static void share(Activity activity, ShareInfo shareInfo, final CallBackListener callBackListener, SHARE_MEDIA share_media) {
        UMShareListener umShareListener = new UMShareListener() {
            /**
             * @descrption 分享开始的回调
             * @param platform 平台类型
             */
            @Override
            public void onStart(SHARE_MEDIA platform) {

            }

            /**
             * @descrption 分享成功的回调
             * @param platform 平台类型
             */
            @Override
            public void onResult(SHARE_MEDIA platform) {
                UIUtils.showToast("分享成功了");
                if (callBackListener != null) {
                    callBackListener.onCallBack();
                }
            }

            /**
             * @descrption 分享失败的回调
             * @param platform 平台类型
             * @param t 错误原因
             */
            @Override
            public void onError(SHARE_MEDIA platform, Throwable t) {
                UIUtils.showToast("分享失败");
                if (callBackListener != null) {
                    callBackListener.onCallBack();
                }
            }

            /**
             * @descrption 分享取消的回调
             * @param platform 平台类型
             */
            @Override
            public void onCancel(SHARE_MEDIA platform) {
                UIUtils.showToast("分享取消了");
                if (callBackListener != null) {
                    callBackListener.onCallBack();
                }
            }
        };

        if (!TextUtils.isEmpty(shareInfo.getTargetUrl())) {
            UMImage image = new UMImage(activity, shareInfo.getImageUrl());//图片
            image.setThumb(image);  //缩略图
            UMWeb web = new UMWeb(shareInfo.getTargetUrl());
            web.setTitle(shareInfo.getTitle());//标题
            web.setDescription(shareInfo.getText());//描述
            web.setThumb(image);
            new ShareAction(activity)
                    .setCallback(umShareListener)
                    .setDisplayList(share_media)
                    .withMedia(web)
                    .share();
        } else {
            if (!TextUtils.isEmpty(shareInfo.getImageUrl())) {
                UMImage image = new UMImage(activity, new File(shareInfo.getImageUrl()));//图片
                image.setThumb(image);  //缩略图
                new ShareAction(activity)
                        .setCallback(umShareListener)
                        .setPlatform(share_media)
                        .withMedia(image)
                        .share();
            }
        }


    }


    public interface CallBackListener {
        void onCallBack();
    }


}
